var SpriteManager = require("SpriteManager");

cc.Class({
    extends: cc.Component,

    properties: {
        scoreDisplay: {
            default: null,
            type: cc.Label
        },
		targetDisplay: {
            default: null,
            type: cc.Label
        },
		messageDisplay: {
            default: null,
            type: cc.Label
        },
		nextDisplay: {
            default: null,
            type: cc.Label
        },
		backDisplay: {
            default: null,
            type: cc.Label
        }
    },

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
		 
		   this.spriteManager = new SpriteManager();
		 
		   this.scoreAddCallback = cc.callFunc(function () {
				this.addStart();
		   }, this); 
		   
           		   
		   this.score = window.score;
		   this.levelTarget = cc.sys.localStorage.getItem("hightestScore")?cc.sys.localStorage.getItem("hightestScore"):0;
		   this.level = window.level ;
		   this.lastScore = window.lastScore;
		   
		   this.scoreAdd = Math.floor(this.score/120);
		 
		   this.scoreDisplay.string = '本局得分 : '+this.lastScore;
		   this.targetDisplay.string = '历史最高 : '+this.levelTarget;
		   
		   this.scoreDisplay.node.setPosition(300,120);
		   this.targetDisplay.node.setPosition(300,0);
		   
		   this.scoreDisplay.node.runAction(cc.moveBy(0.2,-300,0));
		   this.targetDisplay.node.runAction(cc.sequence(cc.moveBy(0.2,-300,0),cc.delayTime(0.1),this.scoreAddCallback ));
		   
		   this.flag = true;
		   
		   this.messageDisplay.node.active = false;
		   this.nextDisplay.node.active = false;
		   this.backDisplay.node.active = false;
		   
		   //this.failSpriteFrame = this.spriteManager.getSprite(200,406,105,170,this.resource);
		   
		   //this.successSpriteFrame = this.spriteManager.getSprite(200,822,155,202,this.resource);
		   
		   //var node = this.spriteManager.createNode(177.5,470,this.successSpriteFrame,0.7,this);
		   
		   //console.log(node);
		   
		   
	 },
	 
	 update(dt){
		 if(!this.flag){
			 if(this.lastScore<this.score){
				 if(this.score-this.lastScore>this.scoreAdd)
					this.lastScore+=this.scoreAdd;
				 else
					 this.lastScore=this.score;
				 this.scoreDisplay.string = '本局得分 : '+this.lastScore;
			 }else{
				 this.addOver();
				 this.flag=true; 
			 } 
		 }
	 },
	 
	 addStart(){
		this.flag=false; 
	 },
	 
	 addOver(){
		 
		 this.toScoreCallback = cc.callFunc(function () {
				this.showResult();
		}, this); 
		 
		 if(this.score>=this.levelTarget){
			 
			 this.messageDisplay.string = '新纪录';
			 
			 //this.nextDisplay.string = 'next';
			 
			 this.scoreDisplay.node.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,1,1), this.toScoreCallback));
			 
			  cc.sys.localStorage.setItem("hightestScore",this.score);
			 
		 }else{
			 
			 this.messageDisplay.string = '游戏结束';
			 
			 //this.nextDisplay.string = 'again';
			 
			 this.targetDisplay.node.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,1,1), this.toScoreCallback));
			 
		}
		 
		window.level=0;
		window.score=0;
		window.lastScore = 0;
		 
		 
	 },
	 
	 showResult(){
		this.messageDisplay.node.active = true;
		this.nextDisplay.node.active = true;
		this.backDisplay.node.active = true;
		/*
		if(this.score>=this.levelTarget)
			this.spriteManager.createNode(177.5,470,this.successSpriteFrame,0.7,this);
		else
			this.spriteManager.createNode(177.5,470,this.failSpriteFrame,0.7,this);*/
		this.nextDisplay.node.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5,1.2,1.2),cc.scaleTo(0.5,1,1))));
	 }
});
